package com.tensquare.base.web.controller;

import com.tensquare.base.po.Label;
import com.tensquare.base.service.LabelService;
import constants.StatusCode;
import dto.PageResultDTO;
import dto.ResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/label")
@CrossOrigin//支持跨域请求
public class LabelController {

    @Autowired
    private LabelService labelService;

    /*
    * 条件查询
    * */
 /*   @PostMapping("/search")
    public ResultDTO list(@RequestBody Map<String,Object> searchMap){
        List<Label> labelList = (List<Label>) labelService.getLabelSpecification(searchMap);
        return new ResultDTO(true,StatusCode.OK,"查询成功",labelList);
    }*/
    /*
    * 分页
    * */
    @PostMapping("/search/{page}/{size}")
    public ResultDTO page(@RequestBody Map<String,Object> map,@PathVariable int page,@PathVariable int size){
        Page<Label> page1 = labelService.page(map, page, size);
        PageResultDTO data = new PageResultDTO(page1.getTotalElements(),page1.getContent());
        return new ResultDTO(true,StatusCode.OK,"分页成功",page1.getContent());
    }

    /*
    * 添加一个
    * */
    @PostMapping
    public ResultDTO add(@RequestBody Label label){
        labelService.saveLabel(label);
        return new ResultDTO(true,StatusCode.OK,"添加成功");
    }
    /*
    * 修改编辑
    * */
    @PutMapping("/{id}")
    public ResultDTO edit(@RequestBody Label label,@PathVariable String id){
        label.setId(id);
        labelService.updateLabel(label);
        return new ResultDTO(true,StatusCode.OK,"修改成功");
    }

    /*
    * 根据id删除一个
    * */
    @DeleteMapping("/{id}")
    public ResultDTO remove(@PathVariable String id){
        labelService.deleteLabelById(id);
        return new ResultDTO(true,StatusCode.OK,"删除成功");
    }

    /*
    * 查询所有
    * */
    @GetMapping
    public ResultDTO list(){
        /*int d=1/0;*/
        List<Label> labelList = labelService.findLabelList();
        return new ResultDTO(true,StatusCode.OK,"查询所有",labelList);
    }

    /*
    * 根据id查询一个
    * */
    @GetMapping("/{id}")
    public ResultDTO listById(@PathVariable String id, HttpServletRequest request){
        System.out.println("当前服务端口："+request.getServerPort());
        Label labelById = labelService.findLabelById(id);
        return new ResultDTO(true,StatusCode.OK,"查询成功",labelById);
    }
}
